package com.example.springbootjspshiro.Realm;

import com.example.springbootjspshiro.pojo.Perms;
import com.example.springbootjspshiro.pojo.Role;
import com.example.springbootjspshiro.pojo.User;
import com.example.springbootjspshiro.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;


//自定义realm
public class CustomerRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;

    //认证阶段
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        System.out.println("=============");

        //从传过来的token获取到的用户名
        String principal = (String) token.getPrincipal();
        System.out.println("用户名"+principal);
        //用这个用户名去数据库查找该用户的信息
        User loginUser = userService.Login(principal);
        if (!ObjectUtils.isEmpty(loginUser)){//如果对象不为空
// 返回数据查到的
            return new SimpleAuthenticationInfo(loginUser.getUsername(),//名字
                    loginUser.getPassword(),//密码
                    ByteSource.Util.bytes(loginUser.getSalt()),//随机盐
                    this.getName());
        }

        return null;
    }
    //授权阶段
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();//要返回的实例类
//========================================授权角色信息===============================================
        //获取主身份信息
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        System.out.println("调用授权验证");
        User user = userService.findUseRolesByUsername(primaryPrincipal);
        //根据这个用户名去数据库中找他的权限并加给他
        if(!CollectionUtils.isEmpty(user.getRoles())){
            List<Role> roles = user.getRoles();
            for (Role role : roles) {
                simpleAuthorizationInfo.addRole(role.getName());
                List<Perms> permsLists = userService.findPermsByRoleId(role.getId());
                if(!CollectionUtils.isEmpty(permsLists)){
                    for (Perms perms : permsLists) {
                        simpleAuthorizationInfo.addStringPermission(perms.getName());
                    }
                }
            }
        }
//        simpleAuthorizationInfo.addStringPermission("order:*:*");//设置资源权限


        return simpleAuthorizationInfo;
    }

}

